home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / Drtf / DPICTHandler.cpp < prev    next >
Text File  |  1996-07-05  |  7KB  |  301 lines

  1. // DPICTHandler.cpp
  2. // d.g.gilbert
  3.  
  4.  
  5. #include "Dvibrant.h"
  6. #include "DFile.h"
  7. #include "DPICTHandler.h"
  8. #include "DPICTprocess.h"
  9. #include "DRichViewNu.h" //  DRichViewP
  10. #include "DDrawPICT.h"   // DDrawPICTst
  11. #include "DRichMoreStyle.h"
  12.  
  13.  
  14.  
  15.  
  16. //class  DPICTHandler
  17.  
  18. DPICTHandler::DPICTHandler(DRichView* itsDoc, DFile* savefile) :
  19.     DRichHandler( itsDoc, savefile)
  20. {
  21.     fFormatName = "PICT";
  22.     fMinDataToProcess= 612;
  23.     if (fDocFile) fDocFile->Open("wb"); // make sure savefile is binary
  24. }
  25.  
  26. char* DPICTHandler::IsRich(char* buf, ulong buflen)
  27. {
  28.     DDrawPict* aDrawPict = new DDrawPict();
  29.     char* picat = (char*) aDrawPict->IsPICT(buf, buflen, (Nlm_Boolean)false/*dontTestForHead*/);
  30.     delete aDrawPict;
  31.     return picat;
  32. }
  33.  
  34. DRichprocess* DPICTHandler::NewProcessor( DFile* itsFile, Nlm_MonitorPtr progress)
  35. {
  36.         fProcessor = new DPICTprocess( itsFile, fDocFile, fDoc, progress);
  37.         return fProcessor;
  38. }
  39.  
  40. void DPICTHandler::InstallInStyle( DRichView* theDoc, DRichStyle* theStyle)
  41. {
  42.     if (fProcessor) {
  43.         ((DPICTprocess*)fProcessor)->InstallPicStyle(theStyle);  
  44.         delete fProcessor; 
  45.         fProcessor= NULL; 
  46.             // must search thru all paragraphs in theDoc and update item heights for
  47.         theDoc->UpdateStyleHeight(theStyle);
  48.         theDoc->SetAutoAdjust(true); 
  49.         //theDoc->AdjustScroll();
  50.         }  
  51. }
  52.  
  53.  
  54. Boolean DPICTHandler::ProcessData( char* cbeg, char* cend, Boolean endOfData, 
  55.                     ulong& dataRemaining) 
  56. {
  57.  
  58.     if (Format() == kPICTformat && fProcessStage == kAtStart && fDocFile) {
  59.             // write empty pict header !?
  60.         char zerobuf[512];
  61.         ulong count= 512;
  62.         Nlm_MemFill( zerobuf, 0, count);
  63.         fDocFile->WriteData( zerobuf, count);
  64.         fProcessStage= kAfterHeader;
  65.         }
  66.  
  67. #if 1            
  68.     if (!fProcessor) { 
  69.         fDoc->SetAutoAdjust(false);
  70.         fProcessor= this->NewProcessor((DFile*)NULL,(Nlm_MonitorPtr)NULL); 
  71.         fProcessStage = kInBody;
  72.         }
  73.  
  74.     if (dataRemaining && fDocFile) {
  75.         ulong count= dataRemaining;  
  76.         fDocFile->WriteData( cbeg, count);
  77.         dataRemaining= 0;
  78.         }
  79.         
  80.     if (endOfData && fProcessor) { 
  81.         if (fDocFile) fDocFile->Close();
  82.     
  83.         switch (fTasknum) {
  84.         
  85.             case 1: // inline image
  86.                 // fProcessor->InstallPicStyle(theStyle); is called by others
  87.                 break;
  88.                 
  89.             default: 
  90.                 fProcessor->Close(); 
  91.                 if (fProcessor->GetTitle()) fDoc->SetTitle( (char*)fProcessor->GetTitle());
  92.                 delete fProcessor; 
  93.                 fProcessor= NULL; 
  94.                 fDoc->AdjustScroll();
  95.                 fDoc->SetAutoAdjust(true);   
  96.                 break;
  97.             }
  98.             
  99.         return true;
  100.         } 
  101.     return 3; //<< flag update date but not view; // true;
  102.     
  103. #else    
  104.  
  105.             // Dgg_DrawPict is not currently written to draw partial PICT data
  106.             // So for now, this handler should always return false until endOfData is true
  107.     if (!endOfData) return false;
  108.  
  109.     return DRichHandler::ProcessData( cbeg, cend, endOfData, dataRemaining);        
  110. #endif
  111. }
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120. //class  DPICTprocess
  121.  
  122.  
  123. DPICTprocess::DPICTprocess( DFile* itsFile, DFile* itsTempData, 
  124.                                         DRichView* itsDoc, Nlm_MonitorPtr progress) :
  125.         DRichprocess( itsFile, itsDoc, progress),
  126.         fPictMax(0), fPictSize(0), fPict(NULL), 
  127.         fTempData( itsTempData),
  128.         fNotDone(true), fInPic(true) 
  129. {     
  130. }
  131.     
  132.  
  133. DPICTprocess::~DPICTprocess()
  134. {
  135.     //////// nooooo //// Nlm_MemFree(fPict); // data is passed to displayer
  136. }
  137.  
  138. void DPICTprocess::Close()
  139. {
  140.     (void) PutPicture();
  141. }
  142.  
  143. void DPICTprocess::Read()
  144. {
  145.     (void) PutPicture();
  146. }
  147.  
  148.  
  149.  
  150. void DPICTprocess::PutPicChar(short c)
  151. {
  152.     if (1 + fPictSize >= fPictMax) {
  153.         fPictMax = fPictSize + 1025;
  154.         if (!fPict) fPict= (char*) Nlm_MemNew(fPictMax);
  155.         else fPict= (char*) Nlm_MemMore( fPict, fPictMax);
  156.         }
  157.     if (fPict) fPict[fPictSize++]= (char) c;
  158. }
  159.  
  160.  
  161. void DPICTprocess::handleTextClass()
  162. {
  163.     if (fInPic) {
  164.         PutPicChar( fMajor);
  165.         }
  166.     else {
  167.         if (IsNewStyle()) StoreStyle(fOldStyleRec.style, FALSE);  
  168.         PutLitChar(fMinor); 
  169.         fLastChar= fMajor; //??
  170.         }
  171. }
  172.  
  173. void DPICTprocess::handleControlClass()
  174. {
  175.     if (fInPic) 
  176.         PutPicChar( fMinor);
  177.     else  
  178.         DRichprocess::handleControlClass();
  179. }
  180.  
  181.  
  182. Boolean DPICTprocess::StuffInStyle( DRichStyle* theStyle)
  183. {            
  184.     if (fPictSize) {
  185.         fPict[fPictSize]= 0;
  186.         DPictStyle* newPict= 
  187.                 new DPictStyle( DPictStyle::kPictMac, fPict, fPictSize, true);  
  188.         Nlm_RecT picrect;
  189.         DDrawPict* aDrawPict = new DDrawPict();
  190.         aDrawPict->GetFrame( &picrect, fPict, fPictSize, DDrawPict::kPictMac);
  191.         delete aDrawPict;
  192.         theStyle->pixwidth= picrect.right - picrect.left;
  193.         theStyle->pixheight= picrect.bottom - picrect.top;
  194.  
  195.         DPictStyle* oldPict= (DPictStyle*) theStyle->fObject;
  196.         if (oldPict) {
  197.             Nlm_RecT loc;
  198.             // netpict info we need to keep ?!
  199.             // this is an html pict!?, reset loc for netlinks 
  200.             Boolean resetLoc= (oldPict->fLinks[0].fKind == DPictStyle::kPictNetPic);
  201.             short i, n = oldPict->fNumLinks;
  202.             for (i= 0; i<n; i++) {
  203.                 if (resetLoc) loc = picrect;
  204.                 else loc= oldPict->fLinks[i].fLoc;
  205.                 if (oldPict->fLinks[i].fKind == DPictStyle::kPictNetLink
  206.                  || oldPict->fLinks[i].fKind == DPictStyle::kPictMapLink)
  207.                     newPict->AddLink( oldPict->fLinks[i].fKind, oldPict->fLinks[i].fLinkid, loc);
  208.                 }
  209.             oldPict->suicide(); // ?? delete
  210.             }
  211.         theStyle->ispict= TRUE;
  212.         theStyle->fObject= newPict;
  213.         
  214.         fPictSize= 0;
  215.         fPict= NULL; //??
  216.         return true;
  217.         }
  218.     else
  219.         return false;
  220. }
  221.  
  222. void DPICTprocess::ReadDataFromFile()
  223. {    
  224.     if (!fDataFile) fDataFile= fTempData;
  225.     if (fNotDone && fDataFile) {
  226.         ulong filelen, fileindex;
  227.         fDataFile->Close(); // !?!? are we done writing to fTempData ??
  228.         fDataFile->Open("rb"); 
  229.         fDataFile->GetDataLength( filelen);
  230.         
  231.         fPictMax= filelen+1;
  232.         fPict= (char*) Nlm_MemNew(fPictMax);        
  233.  
  234.             //// need to skip MacDraw 512 byte header, if it exists 
  235.         fPictSize= MIN(filelen, 1024);
  236.         fDataFile->ReadData( fPict, fPictSize);
  237.         DDrawPict* aDrawPict = new DDrawPict();
  238.         char* picat = (char*) aDrawPict->IsPICT( fPict, fPictSize, false/*dontTestForHead*/);
  239.         delete aDrawPict;
  240.         fileindex= picat - fPict;
  241.         fDataFile->SetDataMark( fileindex);
  242.         fPictSize= filelen - fileindex;
  243.         fDataFile->ReadData( fPict, fPictSize);
  244.         }
  245. }            
  246.         
  247.  
  248. Boolean DPICTprocess::StuffInDocument()
  249. {    
  250.     if (fPictSize) {
  251.         //PutLitStr("\n");  
  252.         //NewParagraph();
  253.  
  254.         (void) StuffInStyle( &fStyleRec.style);
  255.  
  256.         PutLitChar(' '); // (chPictTag); /* put a space for some real text ? */
  257.         StoreStyle(fStyleRec.style, TRUE);
  258.  
  259.         fPictSize= 0;
  260.         fStyleRec.style.fObject= NULL;
  261.         fStyleRec.style.ispict= FALSE;
  262.         fStyleRec.style.pixwidth= 0; 
  263.         fStyleRec.style.pixheight= 0; 
  264.         fOldStyleRec= fStyleRec;
  265.         fOldStyleRec.fontname= NULL; /* ?? force new style ? */
  266.         fInPic= FALSE;
  267.  
  268.         StoreStyle(fStyleRec.style, TRUE);
  269.         PutLitChar('\n'); /* put a space for some real text ? */
  270.         NewParagraph();
  271.  
  272.         //PutLitStr("\n");  
  273.         //NewParagraph();
  274.         return true;
  275.         }
  276.     else
  277.         return false;
  278. }
  279.  
  280.             
  281. Boolean DPICTprocess::InstallPicStyle( DRichStyle* theStyle)
  282. {    
  283.     ReadDataFromFile();
  284.     if (fPictSize && theStyle)  
  285.         return StuffInStyle( theStyle);
  286.     else
  287.         return false;
  288. }
  289.  
  290. Boolean DPICTprocess::PutPicture()
  291. {    
  292.     ReadDataFromFile();
  293.     if (fPictSize)  
  294.         return StuffInDocument();
  295.     else
  296.         return false;
  297. }
  298.  
  299.  
  300.  
  301.